<template>
  <div class="login_container">
    <div class="login_box">
      <!--登录图片区域-->
      <div class="avatar_box">
        <img src="../assets/cug.jpg" alt />
      </div>
      <!--登陆表单区域-->
      <el-form
        ref="loginFormRef"
        label-width="0px"
        :model="loginForm"
        :rules="rules"
        class="login_form"
      >
        <!--用户名-->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <!--验证码-->
        <el-form-item prop="verifycode">
          <el-input v-model="loginForm.verifycode" placeholder="点击图片更换验证码" prefix-icon="el-icon-info" style="width: 310px"></el-input>
          <div @click="refreshCode">
            <W-VerifyCode :identifyCode="identifyCode"></W-VerifyCode>
          </div>
        </el-form-item>
        <!--按钮区域-->
        <el-form-item class="btns">
          <el-button @click="Forget">忘记密码</el-button>
          <el-button type="primary" @click="Login">登录</el-button>
          <el-button type="success" @click="Register">注册</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import VerifyCode from './login/verifycode'
export default {
  data () {
    return {
      // 这是登陆表单数据绑定对象
      loginForm: {
        username: '',
        password: '',
        verifycode: ''
      },
      identifyCodes: '1234567890abcdefjhijklinopqrsduvwxyz',
      identifyCode: '',
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 16, message: '长度在3-12个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 16, message: '长度在4-12个字符', trigger: 'blur' }
        ],
        verifycode: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 4, max: 4, message: '长度为4个字符', trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    'W-VerifyCode': VerifyCode
  },
  methods: {
    // 重置登陆表单
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    },
    Login () {
      if (this.loginForm.verifycode.toLowerCase() !== this.identifyCode.toLowerCase()) {
        this.$message.error('请填写正确验证码')
        this.refreshCode()
        return
      }
      var User = { username: this.loginForm.username, password: this.loginForm.password }
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$axios.post('http://localhost:8081/login', User)
        console.log(res.data)
        if (res.code !== 200) this.$message.error('登陆失败，请检查用户名或密码!')
        else if (res.code === 401) this.$message.error('账户还未激活，请激活后使用!')
        this.$message.success('登陆成功!')
        window.sessionStorage.setItem('token', res.token)
        window.sessionStorage.setItem('username', this.loginForm.username)
        window.sessionStorage.setItem('uid', res.uid)
        window.sessionStorage.setItem('avatar', res.avatar)
        this.$router.push('/home')
      })
      // this.$refs.loginFormRef.validate(valid => {
      // if (!valid) return this.$message.error('登录失败！')
      // this.$message.success('登陆成功!')
      // this.$router.push('/home')
      // })
    },
    Register () {
      this.$router.push('/register')
    },
    Forget () {
      this.$router.push('/forget')
    },
    refreshCode () {
      this.identifyCode = ''
      this.makeCode(this.identifyCodes, 4)
    },
    makeCode (o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.identifyCodes[this.randomNum(0, this.identifyCodes.length)]
      }
    },
    randomNum (min, max) {
      return Math.floor(Math.random() * (max - min) + min)
    }
  },
  mounted () {
    // 初始化验证码
    this.identifyCode = ''
    this.makeCode(this.identifyCodes, 4)
  }
}
</script>

<style>
.el-form-item__content {
  display: flex;
  align-items: center;
}
</style>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background-image: url('../assets/University.png');
  background-repeat: no-repeat;
  background-size:100% 100%;
}
.login_box {
  width: 450px;
  height: 330px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
